package com.ht.dsp;

import com.ht.aems.dbservice.ConnectionFactory;
import com.ht.aems.fesmodel.FesPersistenceFactory;
import com.ht.aems.ntp.CIMDataUpdator;
import com.ht.aems.ntp.SystemModel;
import com.ht.aems.ntp.impl.MeasValueProvider_nari;
import com.ht.dsp.impl.RemoteDspFacade;
import com.ht.dsp.impl.ScadaDataUpdator_file;
import com.ht.memcached.MemCachedConfigurator;
import hhh.remoteService.utils.ServiceRegister;
import hhh.remoteService.utils.rmi.RmiRegistry;
import hhh.utils.FileUtil;
import hhh.utils.LogUtil;
import rtdb.RtDataHandler_MemCached;
import rtdb.RtMeasureHandler_MemoCached;

import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;

/**
 * This main method is used for test using file
 *
 * @author Dong Shufeng
 *         Date: 2011-3-22
 */
public class Main4Test {
    public static void main(String[] args) throws IOException, InterruptedException, ParseException {
        LogUtil.initialLog4j("dsp/dsp.log");
        RmiRegistry.setSpecifiedSocket();

        //load memcached config file
        String f = FileUtil.getRootDirectory() + "/_config/memcache.properties";
        System.out.println("Load memcached config file : " + f);
        MemCachedConfigurator.config(f);

        //load database access config file
        f = FileUtil.getRootDirectory() + "/_config/dba.properties";
        System.out.println("Load database access config file : " + f);
        ConnectionFactory.setConfig(new FileInputStream(f));

        FesPersistenceFactory.setDatabase("shaems");
        FesPersistenceFactory.setType("sql");
        RemoteDspFacade.instance.setScadaUpdator(new ScadaDataUpdator_file());//set scada updator
        ServiceRegister.registerService(DspFacade.class, RemoteDspFacade.instance, "dsp");

        MeasValueProvider_nari measProvider = new MeasValueProvider_nari();
        measProvider.setMeasId2Analog(DspSysModel.INSTANCE.getScadaAnalog());
        measProvider.setMeasId2Discrete(DspSysModel.INSTANCE.getScadaDiscrete());

        SystemModel.INSTANCE.setMeasProvider(measProvider);
        DspSysModel.INSTANCE.setRtDataHandler(new RtDataHandler_MemCached());
        RtMeasureHandler_MemoCached rtMeasureHandler = new RtMeasureHandler_MemoCached();
        DspSysModel.INSTANCE.setRtMeasureQuery(rtMeasureHandler);
        DspSysModel.INSTANCE.setRtMeasureSaver(rtMeasureHandler);

        CIMDataUpdator updator = new CIMDataUpdator(FileUtil.getRootDirectory() + "/datastore/cim");
        updator.setListener(new DspCimListener(updator));
        updator.startCheckThread();

        //until cim is parsed !
        while (SystemModel.INSTANCE.getCimTimeStamp() == 0) ;

        //just for standalone test
        while (true) {
            RemoteDspFacade.instance.updateScada();
            Thread.sleep(15000);
        }
    }
}
